package cn.bgs.servicevod.controller;

import cn.bgs.serviceutils.utils.CSException;
import cn.bgs.serviceutils.utils.R;
import cn.bgs.servicevod.service.VodService;
import cn.bgs.servicevod.utils.AliyunVodSDKUtils;
import cn.bgs.servicevod.utils.ConstantVodUtils;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/11/16-9:02
 */
@Api(description = "视频点播")
@RestController
@RequestMapping("/vod")
public class VodController {


    @Autowired
    private VodService vodService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @ApiOperation(value = "上传点播视频")
    @PostMapping("uploadAlyiVideo")
    public String uploadAlyiVideo(MultipartFile file) {
        return vodService.uploadVIdeo(file);
    }


    /**
     * 获取视频播放凭证
     * @param videoSourceId
     * @return
     */
    @ApiOperation(value = "获取视频播放凭证")
    @GetMapping("getPlayAuth/{videoSourceId}")
    public R getPlayAuth(@PathVariable String videoSourceId) {
        try {
            //创建初始化对象
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取凭证的request和response对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //向request对象中设置视频id
            request.setVideoId(videoSourceId);
            //调用方法获得凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            return R.ok().data("playAuth",response.getPlayAuth());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new CSException(20001,"视频playAuth获取失败");
        }
    }



    /**
     * 获取视频播放凭证
     * @param videoSourceId
     * @return
     */
    @ApiOperation(value = "获取视频播放地址")
    @GetMapping("getPlay/{videoSourceId}")
    public R getPlay(@PathVariable String videoSourceId) {
        try {
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建获取视频地址request和response

            GetPlayInfoRequest request = new GetPlayInfoRequest();
            GetPlayInfoResponse response = new GetPlayInfoResponse();
            //向request对象里面设置视频id
            request.setVideoId(videoSourceId);
            //调用初始化对象里面的方法，传递request，获取数据
            response = client.getAcsResponse(request);

            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            String url="";
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                System.out.println("PlayInfo.PlayURL =" + playInfo.getPlayURL() + "\n");
                url=playInfo.getPlayURL();
            }
            return R.ok().data("url",url);
        } catch (ClientException e) {
            e.printStackTrace();
            throw new CSException(20001,"视频url获取失败");
        }
    }

    /**
     * 根据ID删除视频
     * @param videoId
     * @return
     */
    @ApiOperation(value = "根据ID删除视频")
    @DeleteMapping("/delVod/{videoId}")
    public R removeVideo(@ApiParam(name = "videoId", value = "云端视频id", required = true)
                         @PathVariable String videoId) {

        vodService.removeVideo(videoId);
        return R.ok().message("视频删除成功");
    }
}
